package com.lemon.smartsafety.ui.fragment.safe.data;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;

import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.entity.safe.company.CompanyInfo;
import com.lemon.smartsafety.http.service.SafeDataServices;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.ui.adapter.safe.data.SafeDataCompanyInfoAdapter;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshFragment;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafety.view.list.MultiStateView;

import java.util.List;

import kale.adapter.LemonRcvAdapter;

/**
 * Created by Cooliean on 16/2/21.
 */
public class SafeDataCollectionMainFragment extends BasePullToRefreshFragment<CompanyInfo, List<CompanyInfo>>
        implements RecyclerTClickListener<CompanyInfo> {
    private LoggerHelper loggerHelper = new LoggerHelper(SafeDataCollectionMainFragment.class);




    public static SafeDataCollectionMainFragment newInstance(){
        SafeDataCollectionMainFragment  fragment = new SafeDataCollectionMainFragment();
        return fragment;
    }

    @Override
    protected void initViews() {
        super.initViews();
        //mCompanyInfoDao = AppService.getDBHelper().getDaoSession().getCompanyInfoDao();
        pageSize = 30;

        mMultiStateView.setViewState(MultiStateView.VIEW_STATE_LOADING);
        this.onPullRefresh();
    }

    @Override
    public boolean goRefreshOnCreate() {
        return false;
    }

    @Override
    public LemonRcvAdapter<CompanyInfo> getLemonAdapter() {
        if (mAdapter != null) return mAdapter;
        return new SafeDataCompanyInfoAdapter(mDataList, this);
    }

    @Override
    public String getHttpTag() {
        return EventTag.TAG_SAFE_DATA_COLLECTION_COMPANY_RECENT;
    }


    @Override
    public void onLoadMore() {
        SafeDataServices.init().getRecentCompanyInfo(RefreshType.LOAD_MORE, pageSize, currentPage);
    }

    @Override
    public void onPullRefresh() {
        SafeDataServices.init().getRecentCompanyInfo(RefreshType.REFRESH, pageSize, currentPage);
    }


    @Override
    public void httpRefresh(List<CompanyInfo> data) {
        super.httpRefresh(data);
    }

    @Override
    public void httpLoadMore(List<CompanyInfo> data) {
        super.httpLoadMore(data);
    }

    @Override
    public void httpRefreshError(String msg) {
        super.httpRefreshError(msg);
    }

    @Override
    public void httpLoadMoreError(String msg) {
        super.httpLoadMoreError(msg);
    }

    @Override
    public void onItemClick(CompanyInfo data, int position) {
        if (StringUtils.nullValue(data.getCompany_code()).length() != 0) {
            SafeDataServices.init().setRecentCompany(data.getCompany_code());
        }
        Bundle bundle = new Bundle();
        bundle.putSerializable(BundleKey.COMPANY_INFO, data);
        UIHelper.showSafeDataCollectionMainPanel(getActivity(), bundle);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
    }


    public void doSearch(String key) {
//        if (key.length() != 0) {
//            mPullToRefreshRecyclerView.setRefreshMode(Mode.DISABLED);
//        }else{
//            mPullToRefreshRecyclerView.setRefreshMode(Mode.PULL_FROM_START);
//            if (mDataList.size() != 0) {
//                mPullToRefreshRecyclerView.setFooterComplete();
//            }
//        }
//        QueryBuilder<CompanyInfo> builder = mCompanyInfoDao.queryBuilder().whereOr(CompanyInfoDao.Properties.Company_name.like("%"+key+"%"),CompanyInfoDao.Properties.Company_code.like("%"+key+"%"));
//        List<CompanyInfo> result = builder.list();
//        mAdapter.clear(mDataList);
//        if (null != result) {
//            mAdapter.addItems(result);
//        }
    }

}
